package com.jkl.comm.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.core.annotation.Order;

import com.alibaba.fastjson.JSON;
import com.jkl.entity.BaseResponseDTO;

@Order(20)
@WebFilter(urlPatterns = { "/jky/*" })
public class LoginFilter implements Filter {

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
			throws IOException, ServletException {
		if (servletRequest instanceof HttpServletRequest) {
			HttpServletRequest request = (HttpServletRequest) servletRequest;
			String path = request.getRequestURL().toString();
			if (path.contains("login") || path.contains("getImage") || path.contains("validImage")
			    || path.contains("syncGarbageWeightingInfo") ) {
				filterChain.doFilter(servletRequest, servletResponse);
				return;
			} 
			/*
			 * else if (request.getSession().getAttribute("user") == null) { BaseResponseDTO
			 * result = new BaseResponseDTO("00001", "用户未登陆2"); String resultStr =
			 * JSON.toJSONString(result); HttpServletResponse response =
			 * (HttpServletResponse) servletResponse; response.setHeader("Content-type",
			 * "application/json;charset=UTF-8"); response.getWriter().write(resultStr);
			 * return; }
			 */
		}
		filterChain.doFilter(servletRequest, servletResponse);
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

}
